package com.locatme.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.locatme.adapter.EventInvitViewAdapter;
import com.locatme.adapter.EventsViewAdapter;
import com.locatme.model.Evenement;
import com.locatme.model.Utilisateur;
import com.locatme.modelBD.EvenementBD;
import com.main.locatme.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Julien on 29/03/14.
 */
public class InvitationsFragment extends Fragment implements View.OnClickListener {

    private EvenementBD evenementBD;
    private List<Evenement> listEvent = new ArrayList<Evenement>();
    private List<String> valuesT = new ArrayList<String>();
    private List<String> valuesInvit = new ArrayList<String>();
    private Utilisateur user;
    private ListView listView, listeInvit;
    private int index;
    private LayoutInflater layoutInflater;
    private View rootView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        layoutInflater = inflater;
        rootView = inflater.inflate(R.layout.fragment_invitations, container, false);
        listView = (ListView) rootView.findViewById(R.id.listInvit);
        user = (Utilisateur) getActivity().getIntent().getExtras().getParcelable("user");
        evenementBD = new EvenementBD(this.getActivity());

        //Mise en place de l'adapter pour le style de la listView
        refreshList();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, final long l) {
                index = i;
                View viewdiag = layoutInflater.inflate(R.layout.dialog_invitation, null);
                TextView eventName = (TextView) viewdiag.findViewById(R.id.evnetname);
                TextView hote = (TextView) viewdiag.findViewById(R.id.host);
                TextView dateDeb = (TextView) viewdiag.findViewById(R.id.startdate);
                TextView dateFin = (TextView) viewdiag.findViewById(R.id.enddate);
                TextView heureDeb = (TextView) viewdiag.findViewById(R.id.starthour);
                TextView heureFin = (TextView) viewdiag.findViewById(R.id.endhour);
                TextView adresse = (TextView) viewdiag.findViewById(R.id.adress);
                TextView descr = (TextView) viewdiag.findViewById(R.id.description);
                Button accept = (Button) viewdiag.findViewById(R.id.buttonAccept);
                Button maybe = (Button) viewdiag.findViewById(R.id.buttonMaybe);
                Button decline = (Button) viewdiag.findViewById(R.id.buttonDecline);
                listeInvit = (ListView) viewdiag.findViewById(R.id.listguest);
                eventName.setText(listEvent.get(i).getIntitule());
                hote.setText(listEvent.get(i).getLoginPersonne());
                dateDeb.setText(listEvent.get(i).getDateDebut());
                dateFin.setText(listEvent.get(i).getDateFin());
                heureDeb.setText(listEvent.get(i).gethDebut());
                heureFin.setText(listEvent.get(i).gethFin());
                adresse.setText(listEvent.get(i).getAdresse());
                descr.setText(listEvent.get(i).getDescription());
                accept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        evenementBD.open();
                        evenementBD.setInvite(listEvent.get(index).getId(), user.getLogin(), 1);
                        evenementBD.close();
                        refreshList();
                        Toast.makeText(InvitationsFragment.this.getActivity(), "Invitation accepted", Toast.LENGTH_LONG).show();
                    }
                });
                maybe.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        evenementBD.open();
                        evenementBD.setInvite(listEvent.get(index).getId(), user.getLogin(), 2);
                        evenementBD.close();
                        refreshList();
                        Toast.makeText(InvitationsFragment.this.getActivity(), "Uncertain invitation", Toast.LENGTH_LONG).show();
                    }
                });
                decline.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        evenementBD.open();
                        evenementBD.setInvite(listEvent.get(index).getId(), user.getLogin(), 3);
                        evenementBD.close();
                        refreshList();
                        Toast.makeText(InvitationsFragment.this.getActivity(), "Invitation declined", Toast.LENGTH_LONG).show();
                    }
                });
                valuesInvit = listEvent.get(i).getListAmis();
                EventInvitViewAdapter adapter = new EventInvitViewAdapter(InvitationsFragment.this.getActivity(), valuesInvit);
                listeInvit.setAdapter(adapter);
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                // Get the layout inflater
                LayoutInflater inflater = getActivity().getLayoutInflater();
                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                builder.setView(viewdiag);
                // Add action buttons
                builder.setNeutralButton("Fermer", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        refreshList();
                    }
                });
                builder.create().show();
            }
        });
        return rootView;
    }

    @Override
    public void onClick(View view) {
    }

    public void refreshList() {
        evenementBD.open();
        listEvent.clear();
        listEvent = evenementBD.recupEvenementParticipants(user.getLogin());
        if (listEvent != null) {
            valuesT.clear();
            valuesInvit.clear();
            for (Evenement e : listEvent) {
                valuesT.add(e.getIntitule() + ";" + evenementBD.recupInviteStatut(e.getId(), user.getLogin()));
            }
        }
        evenementBD.close();
        EventsViewAdapter adapter = new EventsViewAdapter(this.getActivity(), valuesT);
        adapter.setNotifyOnChange(true);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}
